/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HealthMonitoringView;

import Common.Constant;
import DataSource.Data;
import Logger.ILog;
import Logger.LogManager;
import RuleInterence.RuleChecker;
import RuleInterence.RuleCheckerImpl;
import RuleInterence.RuleCheckerListener;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author Tuan
 */
public class MonitoringForm extends AbstractForm implements RuleCheckerListener{
       
    private TextField txtSBP;
    private Gauge gSBP;    
    private TextField txtDBP;
    private Gauge gDBP;
    private TextField txtHeartBeat;
    private Gauge gHeartBeat;
    private TextField txtTemperatue;
    private Gauge gTemp;
    private StringItem txtSymptom;
    private StringItem txtSummary;
    private RuleChecker ruleChecker;
    private StringBuffer buffer;
    private static final ILog logger = LogManager.GetLogger(MonitoringForm.class.getName());
    
    public MonitoringForm(MIDletNavigation midlet, RuleChecker ruleChecker){
        super("Monitoring Your Health", null, midlet);             
        this.ruleChecker = ruleChecker;
        this.ruleChecker.registerCallback(this);
        this.buffer = new StringBuffer();      
    }
    
    protected void initialize(){
        super.initialize();
        txtSBP = new TextField("Systolic(mmHg)", null, 32, TextField.ANY);     
        txtDBP = new TextField("Diastolic(mmHg)", null, 32, TextField.ANY);
        txtHeartBeat = new TextField("Heart Beat(bpm)", null, 32, TextField.ANY);
        txtTemperatue = new TextField("Temperature(Celcius)", null, 32, TextField.ANY);
        txtSymptom = new StringItem("", "");        
        this.append("Diagnosing...");
        this.append("Your condition is \n");      
        this.txtSymptom = new StringItem("", "");
        this.txtSymptom.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM));
        
        this.txtSummary = new StringItem("", "");
        
        gSBP = new Gauge("", false, 100, 0);
        gDBP = new Gauge("", false, 100, 0);
        gHeartBeat = new Gauge("", false, 100, 0);
        gTemp = new Gauge("", false, 100, 0);
        
        this.append(txtSymptom);    
        this.append(txtSummary);
        this.append(txtSBP);
        this.append(gSBP);
        this.append(txtDBP);
        this.append(gDBP);
        this.append(txtHeartBeat);
        this.append(gHeartBeat);
        this.append(txtTemperatue);  
        this.append(gTemp);       
        
    }   

    /**
     * @param ruleChecker the ruleChecker to set
     */
    public void setRuleChecker(RuleCheckerImpl ruleChecker) {
        this.ruleChecker = ruleChecker;
        this.ruleChecker.registerCallback(this);
    }

    public void onComplete(String result, Data data) {
        buffer.setLength(0);        
        this.txtSBP.setString(buffer.append(data.getSystolic()).append("/").append(Constant.MAX_SBP).toString());        
        this.gSBP.setValue(data.getSystolic()*100/Constant.MAX_SBP);
        
        buffer.setLength(0);
        this.txtSummary.setText(buffer.append("(").append(data.getSystolic()).append(", ").append(data.getDiastolic()).append(", ").append(data.getHeartBeat()).append(", ").append(data.getTemperature()).append(")").toString());
        
        buffer.setLength(0);
        this.txtDBP.setString(buffer.append(data.getDiastolic()).append("/").append(Constant.MAX_DBP).toString());
        this.gDBP.setValue(data.getDiastolic()*100/Constant.MAX_DBP);
        
        buffer.setLength(0);
        this.txtHeartBeat.setString(buffer.append(data.getHeartBeat()).append("/").append(Constant.MAX_HEART_BEAT).toString());
        this.gHeartBeat.setValue(data.getHeartBeat()*100/Constant.MAX_HEART_BEAT);
        
        buffer.setLength(0);
        this.txtTemperatue.setString(buffer.append((int)data.getTemperature()).append("/").append(Constant.MAX_TEMP).toString());
        this.gTemp.setValue((int)data.getTemperature()*100/Constant.MAX_TEMP);              
        this.txtSymptom.setText(result + "\n");
        
        logger.info(data.toString() + " => " + result);
    }

}
